﻿using AnlianCommon.Common.Domains.BankCustomers;
using AnlianCommon.Common.DTO;
using AnlianCommon.Common.Infrastructure.IRepository;
using AnlianCommon.Enum;
using AutoMapper;
using MediatR;
using Yitter.IdGenerator;

namespace SheepCommon.API.Vendors
{
    public class GPpriceCreateHander : IRequestHandler<GPpriceCreateCommand, Result>
    {
        private readonly IBaseRepository<GPprice> gpPriceRepository;
        private readonly IMapper mapper;

        public GPpriceCreateHander(IBaseRepository<GPprice> gpPriceRepository, IMapper mapper)
        {
            this.gpPriceRepository = gpPriceRepository;
            this.mapper = mapper;
        }

        /// <summary>
        /// 商品价格创建
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<Result> Handle(GPpriceCreateCommand request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCode.Added };
            try
            {
                var gpprice = mapper.Map<GPprice>(request);
                var list = await gpPriceRepository.Add(gpprice);
                if (list > 0)
                {
                    gpprice.CreatedOwnerId = "张三";
                    gpprice.CreatedTime = DateTime.Now;
                    result.Code = (int)CommonErrorCode.Success;
                    result.Msg = "添加成功";
                }
                else
                {
                    result.Msg = "添加失败";
                }
            }
            catch (Exception ex)
            {
                throw;
            }
            return result;
        }
    }
}
